package com.jy.api.service.impl;

import com.jy.api.dao.CouponDao;
import com.jy.api.domain.Coupon;
import com.jy.api.enums.DistributeType;
import com.jy.api.exception.MyException;
import com.jy.api.service.CouponInstanceService;
import com.jy.api.service.CouponService;
import com.jy.api.service.MemberService;
import com.jy.api.signUpReq.admin.CouponSignUpReq;
import com.jy.api.signUpReq.admin.DistributeCouponSignUpReq;
import com.jy.api.util.MyDateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by lihu on 2017/11/17.
 */
@Slf4j
@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    CouponDao couponDao;

    @Autowired
    CouponInstanceService couponInstanceService;

    @Autowired
    MemberService memberService;

    @Override
    @Transactional
    public void saveOrUpdate(CouponSignUpReq couponSignUpReq) {
        //如果优惠卷ID 存在则表示做修改
        Coupon coupon = couponDao.findOne(couponSignUpReq.getId());
        if (coupon == null) {
            coupon = new Coupon();
        }
        //copy couponSignUpReq的值
        BeanUtils.copyProperties(couponSignUpReq, coupon);
        String endDate = couponSignUpReq.getEndDate().concat(" 23:59:59");
        coupon.setStartTime(MyDateUtils.getDateFormatDd(couponSignUpReq.getStartDate(), "yyyy-MM-dd"));
        coupon.setEndTime(MyDateUtils.getDateFormatDd(endDate, "yyyy-MM-dd HH:mm:ss"));
        int days = (int) ((coupon.getEndTime().getTime() - coupon.getStartTime().getTime()) / (1000 * 3600 * 24));
        coupon.setExpireDays(days);
        couponDao.save(coupon);
    }
    @Override
    public Page<Coupon> getCoupon(String storeId, Pageable pageable) {
        return couponDao.findListByStoreIdAndEnabledAndEndTimeAfter(storeId, true, new Date(), pageable);
    }

    @Override
    public Page<Coupon> getCouponAll(String storeId, Pageable pageable) {
        return couponDao.findListByStoreIdAndEnabled(storeId, true, pageable);
    }

    @Override
    @Transactional
    public void deleteCoupon(String id) {
        Coupon coupon = couponDao.findOne(id);
        if (coupon == null) {
            throw new MyException("修改失败！优惠卷信息不存在！");
        }
        coupon.setEnabled(false);
        couponDao.save(coupon);
    }

    @Override
    public Coupon getFondId(String id) {
        return couponDao.findOne(id);
    }

    @Async
    @Override
    public void distributeCoupon(DistributeCouponSignUpReq signUpReq, Coupon coupon,
                                 String creatorId, String creatorName) {
        List<String> memberIds = new ArrayList<>();
        if (DistributeType.SINGLE == signUpReq.getDistributeType()) {
            memberIds = signUpReq.getMemeberIds();
        } else if (DistributeType.ALL == signUpReq.getDistributeType()) {
            memberIds = memberService.getAllMemberId(signUpReq.getStoreId());
        } else if (DistributeType.SLEEP == signUpReq.getDistributeType()) {
            memberIds = memberService.getSleepMemberIds(signUpReq.getStoreId()).
                    stream().map(e -> e.toString()).collect(Collectors.toList());
        }
        for (String memberId : memberIds) {
            try {
                couponInstanceService.addCouponInstance(coupon, memberId, creatorId, creatorName);
            } catch (Exception ex) {
                log.error("distributeCoupon is error:{}", ex);
            }

        }
    }
}
